package com.ruoyi.mp.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.file.FileTypeUtils;
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.mp.domain.MaterialVo;
import com.ruoyi.mp.domain.MpMaterial;
import com.ruoyi.mp.service.IMpAccountService;
import com.ruoyi.mp.service.IMpMaterialService;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 公众号素材Controller
 *
 * @author 郎兴宇
 * @date 2024-11-29
 */
@RestController
@RequestMapping("/material")
public class MpMaterialController extends BaseController
{
    @Autowired
    private IMpMaterialService mpMaterialService;
    @Autowired
    private RemoteFileService remoteFileService;
    @Autowired
    private IMpAccountService iMpAccountService;
    /**
     * 查询公众号素材列表
     */
    @RequiresPermissions("mp:material:list")
    @GetMapping("/list")
    public TableDataInfo list(MpMaterial mpMaterial)
    {
        startPage();
        List<MpMaterial> list = mpMaterialService.selectMpMaterialList(mpMaterial);
        return getDataTable(list);
    }

    /**
     * 导出公众号素材列表
     */
    @RequiresPermissions("mp:material:export")
    @Log(title = "公众号素材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MpMaterial mpMaterial)
    {
        List<MpMaterial> list = mpMaterialService.selectMpMaterialList(mpMaterial);
        ExcelUtil<MpMaterial> util = new ExcelUtil<MpMaterial>(MpMaterial.class);
        util.exportExcel(response, list, "公众号素材数据");
    }

    /**
     * 获取公众号素材详细信息
     */
    @RequiresPermissions("mp:material:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mpMaterialService.selectMpMaterialById(id));
    }

    /**
     * 新增公众号素材
     */
    @RequiresPermissions("mp:material:add")
    @Log(title = "公众号素材", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(MaterialVo materialVo)
    {
        MpMaterial mpMaterial = new MpMaterial();
        if (!materialVo.getFile().isEmpty())
        {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            String extension = FileTypeUtils.getExtension(materialVo.getFile());
            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION))
            {
                return error("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION) + "格式");
            }
            R<SysFile> fileResult = remoteFileService.upload(materialVo.getFile());
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
            {
                return error("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getUrl();

            BeanUtils.copyProperties(materialVo,mpMaterial);
            String appId = this.iMpAccountService.selectMpAccountById(mpMaterial.getAccountId()).getAppId();
            mpMaterial.setAppId(appId);
            mpMaterial.setUrl(url);
        }
        return toAjax(mpMaterialService.insertMpMaterial(mpMaterial));
    }

    /**
     * 修改公众号素材
     */
    @RequiresPermissions("mp:material:edit")
    @Log(title = "公众号素材", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MpMaterial mpMaterial)
    {
        return toAjax(mpMaterialService.updateMpMaterial(mpMaterial));
    }

    /**
     * 删除公众号素材
     */
    @RequiresPermissions("mp:material:remove")
    @Log(title = "公众号素材", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mpMaterialService.deleteMpMaterialByIds(ids));
    }
}
